<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="logo">
      </div>
      <!-- 登录表单页面 -->
      <el-form :model="loginForm"
               :rules="rules"
               ref="loginFormRef"
               class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    prefix-icon="el-icon-user"
                    placeholder="username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    prefix-icon="el-icon-lock"
                    placeholder="password"
                    show-password></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button @click="login" type="primary">登&nbsp;&nbsp;&nbsp;&nbsp;录</el-button>
          <el-button type="info">注&nbsp;&nbsp;&nbsp;&nbsp;册</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    // var validateUsername = (rule, value, callback) => {
    //   this.axios.post('queryUsername', this.loginForm)
    // }
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 15,
            message: '用户名长度长度在5-15个字符',
            trigger: 'blur'
          }
          // ,
          // {
          //   validator: validateUsername,
          //   trigger: 'blur'
          // }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 15,
            message: '密码长度在5-15个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        console.log(valid ? '表单合法' : '表单不合法')
        if (!valid) return
        const { data: res } = await this.$axios.post('login', this.loginForm)
        if (res.meta.status !== 200) this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        // 将登录后服务器返回的token保存到客户端的sessionStorage中
        //  项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //  token只应在当前网站打开期间生效，所以将 token保存在sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        // 通过编程式导航跳转到后台主页，路由地址是/home
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #ffcad4;
  background-image: linear-gradient(340deg, #ffcad4 0%, #ffffee 100%);
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffee;
  border-radius: 20px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #bbbbbb;

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 10px solid #ffedde;
    border-radius: 50%;
    box-shadow: 0 0 10px #bbbbbb;
    left: 50%;
    position: absolute;
    transform: translate(-50%, -70%);

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;

    }
  }

  .el-form-item {
    margin-bottom: 35px;

    .el-input {
      --el-input-border: 3px solid #bbbbbb;
      --el-input-focus-border: #ffcad4;
      --el-input-border-radius: 10px;
      width: 80%;
      line-height: 40px;
    }

    .el-button + .el-button {
      margin-left: 50px;
    }

    .el-button {
      width: 33%;
      border: 2px solid #bbbbbb;
      border-radius: 10px;
    }

    .el-button--primary {
      font-weight: bold;
      --el-button-background-color: rgb(91, 170, 253);
    }

    .el-button--info {
      font-weight: bold;
      --el-button-background-color: #999da5;
    }

  }
}

::v-deep .el-form-item__error {
  color: #ffa0a0;
  font-size: 12px;
  line-height: 1;
  padding-top: 4px;
  position: absolute;
  top: 100%;
  left: 50px;
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
